package example.step1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerialPerson implements Serializable {

    private static final long serialVersionUID = 1041712221752728541L;

    private String name;
    private int age;

    // test
    @Override
    public String toString() {
        return "SerialPerson [name=" + name + ", age=" + age + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 运行函数
    public static void main(String args[]) throws Exception{
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\serial.obj"));
        // 序列化对象
        SerialPerson person = new SerialPerson();
        person.setAge(27);
        person.setName("Lang Yu");
        // 数据的序列化写入
        out.writeBoolean(true);
        out.writeObject(person);
        out.writeInt(120);
        out.writeObject("silentbalanceyh@126.com");
        out.close();
        // 反序列化对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\serial.obj"));
        // 数据的反序列化读取
        System.out.println(in.readBoolean());
        System.out.println(in.readObject());
        System.out.println(in.readInt());
        System.out.println(in.readObject());
        in.close();
    }
}